<?php

namespace app\common\model\car;

use app\common\model\driver\MyDriver;
use app\common\model\user\User;
use think\facade\Request;
use app\common\model\BaseModel;

/**
 * 模型
 */
class Car extends BaseModel
{
    protected $name = 'car';
    protected $pk = 'car_id';

    public function driver()
    {
        return $this->hasOne(MyDriver::Class,'id','driver_id');
    }



    /**
     * 获取全部
     */
    public function getAll()
    {
        $where[] = ['is_delete','=',0];
        return $this->where($where)->order(['create_time' => 'desc'])->select();
    }

    /**
     * 获取用户车辆列表
     */
    public function getList($user_id)
    {   
        $where = [];
        $where['user_id'] = $user_id;
        return $this->where($where)->order(['create_time' => 'desc'])->select();
    }
    /**
     * 详情
     */
    public static function detail($car_id)
    {
        return static::find($car_id);
    }

    /**
     * 获取供应商总数量
     */
    public static function getCarTotalByDay($day)
    {
        $startTime = strtotime($day);
        return self::where('create_time', '>=', $startTime)
            ->where('create_time', '<', $startTime + 86400)
            ->count();
    }

    public static function get_car_conut(){
        return self::where('status', '=', 0)
            ->where('is_delete','=',0)
            ->count();
    }

    public static function CarTotal(){
        return self::where('is_delete', '=', 0)
            ->count();
    }

    public function del($id)
    {
        $res = $this->where(array('car_id'=>$id))->update(array('is_delete'=>1));
        return $res;
    }

    public function get_count($type)
    {
        $times = date_time($type);
        $where[] = ['create_time','between',$times];
        $count = $this->where($where)->count();
        return $count;
    }
  
}